function deleteEvent(id) {
    var result = confirm('This will permantly delete the event. Are you sure?')
    if (result) {
        $.ajax({
            url: 'delete.json',
            dataType: 'json',
            data: {
                id: id
            },
            type: 'POST',
            success: function (json) {
                document.location = 'events.manager';
            },
            error: function () {
                alert('An error occured.');
            }
        })
    }
}

function submitEvent() {
    re = new RegExp(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
    
    var desc = $('#add_event_desc').val();
    var dt = $('#add_event_dt').val();
    
    if (dt.match(re) !== null && desc.length > 0) {
        $.ajax({
            url: 'add.json',
            dataType: 'json',
            data: {
                description: desc,
                datetime: dt
            },
            type: 'POST',
            success: function (json) {
                document.location = 'events.manager';
            },
            error: function () {
                alert('An error occured.');
            }
        })
    }
}

function addEvent() {
    if ($('#add_event').length == 0) {
        $('#events tr:first').after($('<tr id="add_event"><td></td><td><input type="text" id="add_event_desc" /></td><td><input type="text" id="add_event_dt" /></td><td><a href="javascript:submitEvent();">Add</a> | <a href="javascript:cancelAddEvent();">Cancel</a></td></tr>'));
    }
}

function editEvent(id) {
    var rows = $('#events tr');
    for (var i = 0; i < rows.length; i++) {
        var row = rows[i];
        if ($(row).find('td:first').text() == id && $('#edit_event_desc').length == 0) {
            cells = $(row).find('td');
            var description = $(cells[1]).text();
            var datetime = $(cells[2]).text();
            
            $(cells[1]).empty().append('<input type="text" id="edit_event_desc" />');
            $('#edit_event_desc').val(description);
            $(cells[2]).empty().append('<input type="text" id="edit_event_dt" />');
            $('#edit_event_dt').val(datetime);
            $(cells[3]).empty().append('<a href="javascript:updateEditEvent(' + id + ');">Save</a> | <a href="javascript:cancelEditEvent();">Cancel</a>')
        }
    }
}

function updateEditEvent(id) {
    re = new RegExp(/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/);
    
    var desc = $('#edit_event_desc').val();
    var dt = $('#edit_event_dt').val();

    if (dt.match(re) !== null && desc.length > 0) {
        $.ajax({
            url: 'modify.json',
            dataType: 'json',
            data: {
                id: id,
                description: desc,
                datetime: dt
            },
            type: 'POST',
            success: function (json) {
                document.location = 'events.manager';
            },
            error: function () {
                alert('An error occured.');
            }
        })        
    }
}

function cancelEditEvent() {
    document.location = 'events.manager';
}

function cancelAddEvent() {
    $('#add_event').remove();
}
